#include <conio.h>
#include <graphics.h>

#define high 480			// 游戏画面尺寸
#define width 640

// 全局变量
int ball_x,ball_y;			// 小球的坐标
int ball_vx,ball_vy;		// 小球的速度
int radius;					// 小球的半径
int brick_exit[9];			//判断砖块是否存在1为存在0为不存在
int brick_high,brick_width;	//每个砖块的高和宽

//下面定义单板的各项数据
int bar_x,bar_y;	
int bar_left,bar_right,bar_bottom,bar_top;	
int bar_high,bar_width;			

void startup()				// 数据初始化
{
	//下面定义小球的数据
	ball_x  = width/2;
	ball_y  = high/2;
	ball_vx = 1;
	ball_vy = 1;
	radius  = 20;
	
	//下面初始化单板的数据
	bar_high = 15;
	bar_width = 80;
	bar_x = width/3;
	bar_y = high-20;
	bar_top = bar_y - bar_high;
	bar_bottom = bar_y + bar_high;
	bar_left = bar_x - bar_width;
	bar_right = bar_x + bar_width; 

	//下面初始化砖块的数据
	brick_width = width/9;
	brick_high = high/9;
	int i;
	for(i=0;i<9;i++)
		brick_exit[i] = 1;
	
	initgraph(width, high);
	BeginBatchDraw();
}

void clean()				// 消除显示画面
{
	// 绘制黑线、黑色填充的圆
	setcolor(BLACK);
	setfillcolor(BLACK);
	fillcircle(ball_x, ball_y, radius);

	// 绘制黑线、黑色填充的长方形
	setcolor(BLACK);
	setfillcolor(BLACK);
	fillrectangle(bar_left ,bar_top ,bar_right ,bar_bottom );

	//碰撞后砖块变黑
	int i,brick_left,brick_right,brick_top,brick_bottom;
	for(i=0;i<9;i++)
	{
		brick_left = i*brick_width;
		brick_right = brick_left + brick_width;
		brick_top = 0;
		brick_bottom=brick_high;
		if(brick_exit[i] == 0)		//砖块没有时绘制黑色砖块
			fillrectangle(brick_left,brick_top,brick_right,brick_bottom);
	}
}	

void show()					// 显示画面
{
	// 绘制黄线、绿色填充的圆
	setcolor(YELLOW);
	setfillcolor(GREEN);
	fillcircle(ball_x, ball_y, radius);	

	//绘制绿色填充的长方形
	setcolor(YELLOW);
	setfillcolor(GREEN);
	fillrectangle(bar_left ,bar_top ,bar_right ,bar_bottom );
	
	int i,brick_left,brick_right,brick_top,brick_bottom;
	for(i=0;i<9;i++)
	{
		brick_left = i*brick_width;
		brick_right = brick_left + brick_width;
		brick_top = 0;
		brick_bottom=brick_high;

		if(brick_exit[i] == 1)		//砖块有时绘制红色砖块
		{
			setcolor(WHITE);
			setfillcolor(RED);
			fillrectangle(brick_left,brick_top,brick_right ,brick_bottom );
		}
	}
	
	FlushBatchDraw();

	Sleep(1);						// 延时	
}	

void updateWithoutInput()			// 与用户输入无关的更新
{
	// 更新小圆坐标
	ball_x = ball_x + ball_vx;
	ball_y = ball_y + ball_vy;
	
	//碰撞之后反弹
	if ((ball_x <= radius) || (ball_x >= width-radius))
		ball_vx = -ball_vx;
	if ((ball_y <= radius) || (ball_y >= high-radius))
		ball_vy = -ball_vy;	
	if (((ball_y+radius > bar_top) && (ball_y+radius < bar_bottom-bar_high/3)) ||  ((ball_y-radius <= bar_bottom) && (ball_y-radius > bar_top-bar_high/3)))
		if ((ball_x >= bar_left) && (ball_x <= bar_right))
				ball_vy = -ball_vy;

	//下面判断小球是否撞到砖块
	int i,brick_left,brick_right,brick_bottom;
	for (i=0;i<9;i++)
		if(brick_exit[i] == 1)	 //只有砖块存在才进行判断
		{
			brick_left = i*brick_width;
			brick_right = brick_left + brick_width;
			brick_bottom = brick_high;
			if((ball_y==brick_bottom+radius) && (ball_x>=brick_left) && (ball_x<=brick_right))
			{
				brick_exit[i] = 0;
				ball_vy = -ball_vy;	
			}
		}
}

void updateWithInput()		// 与用户输入有关的更新
{	
	MOUSEMSG m;				//定义鼠标消息
	m = GetMouseMsg();		//获取一条鼠标信息

	if(m.uMsg == WM_MOUSEMOVE)
	{
	bar_x = m.x;
	bar_y = m.y;
	bar_left = bar_x - bar_width;
	bar_right = bar_x + bar_width;
	bar_top = bar_y - bar_high;
	bar_bottom = bar_y + bar_high;
	}
}

void gameover()
{
	EndBatchDraw();
	closegraph();
}

int main()
{
	startup();					// 数据初始化	
	while (1)					//  游戏循环执行
	{
		clean();				// 把之前绘制的内容清除
		updateWithoutInput();	// 与用户输入无关的更新
		updateWithInput();		// 与用户输入有关的更新
		show();					// 显示新画面
	}
	gameover();					// 游戏结束、后续处理
	return 0;
}